package com.example.finalthesis.the_db4o_project;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ILIAS on 12/3/2015.
 */
public class ListOFObjects  extends Fragment {

    private List<ItemOfObject> itemList = new ArrayList<ListOFObjects.ItemOfObject>();



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }





    @Override
    public void onAttach(Activity activity) {

        // It's time we check if our activity implements the right inteface
        if (! (activity instanceof ItemListener) )
            throw new ClassCastException();

        super.onAttach(activity);

    }





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.list_item , container, true);
        ListView lView = (ListView) v.findViewById(R.id.listView);
        ItemAdapter la = new ItemAdapter(itemList, getActivity());
        lView.setAdapter(la);
        lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                ( (ItemListener) getActivity()).onChangeItemOfObject( itemList.get(position).ObjectName );
            }
        });
        setHasOptionsMenu(true);
        return v;
    }


/*
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.left_menu, menu);
    }

*/

    private void init() {
        itemList.add(new ItemOfObject("Google"));
        itemList.add(new ItemOfObject("Android dev"));
    }



    public interface ItemListener {
        public void onChangeItemOfObject(String item);
    }

    public class ItemOfObject {
        public String ObjectName;

        public ItemOfObject(String ObjectName) {
            this.ObjectName = ObjectName;
        }
    }
}
